home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / DEBUGTUT.ZIP / DEBUG.TXT
Text File  |  1986-08-22  |  34KB  |  665 lines

  1.  
  2.                                       DEBUG
  3.  
  4.  
  5.      This  tutorial is made to present an overview of the DEBUG.COM program for
  6. the IBM PC.   This utility can be extremely useful, when used correctly.  It is
  7. almost  a  must for Assembler Language programmers,  and can  also  provide  an
  8. insight  into  the operation of the machine at the bit level.   It has  several
  9. nice features, including the ability to display and change any of the registers
  10. in the IBMPC, start and stop program execution at any time, change the program,
  11. and  look  at diskettes,  sector by sector.   DEBUG works at the  machine  code
  12. level,  but it does also have the ability to disassemble machine code,  and (at
  13. dos 2.0), assemble instructions directly into machine code.
  14.  
  15.      The  procedure for starting DEBUG and command syntax will not  be  covered
  16. here,  as they are well documented in the DOS manual.   What we will do is show
  17. some examples of the various commands and the response which is expected.  Note
  18. that the segment registers will probably not be exactly what is shown.  This is
  19. normal, and should be expected.
  20.  
  21.      For  the examples,  I will be using the demo program CLOCK.COM in the  XA4
  22. database.   For  those of you with the IBM assembler (MASM),  the source can be
  23. down loaded.   If you do not have the assembler, or have another assembler, the
  24. file  CLOCK.HEX has been up loaded.   It can be converted to a .COM file  using
  25. any of the existing HEX conversion programs on the SIG.  See the file CLOCK.DOC
  26. for more information.
  27.  
  28.  
  29.  
  30.                                 STARTING DEBUG
  31.  
  32.      There are two ways to start DEBUG with a file.  Both ways produce the same
  33. results, and either can be used.
  34.  
  35.      In the Command Line:  A>debug clock.com <ENTER>
  36.  
  37.      Separate from the command line:  A>debug <ENTER>
  38.                                       -n clock.com
  39.                                       -l
  40.  
  41.      With either method,  you will get the DEBUG prompt of a hyphen (-).  DEBUG
  42. has loaded your program and is ready to run.   The description of each instruc-
  43. tion will assume this as a starting point,  unless otherwise mentioned.   If at
  44. any time you get different results,  check your procedure carefully.   If it is
  45. correct,  please leave me a message.   I have tried to check everything,  but I
  46. have been known to make a mistake or two (anyway).
  47.  
  48.      If you do have problems,  you can enter the command Q (Quit) any time  you
  49. have the DEBUG prompt (-).  This should return you to the DOS prompt.
  50.  
  51.  
  52.                                 RUNNING DEBUG
  53.  
  54.                                DISPLAY COMMANDS
  55.  
  56.  
  57.                                Register command
  58.  
  59.      The first thing we should look at are the registers,  using the R command.
  60. If  you type in an R with no parameters,  the registers should be displayed  as
  61. so:
  62.  
  63. AX=0000  BX=0000  CX=0446  DX=0000  SP=FFFE  BP=0000  SI=0000  DI=0000
  64. DS=6897  ES=6897  SS=6897  CS=6897  IP=0100   NV UP DI PL NZ NA PE NC
  65. 6897:0100 E96B01        JMP     026E
  66.  
  67.      CX  contains  the length of the file (0446h or 1094d).   If the file  were
  68. larger  than 64K,  BX would contain the high order of the size.   This is  very
  69. important to remember when using the Write command,  as this is the size of the
  70. file to be written.   Remember,  once the file is in memory,  DEBUG has no idea
  71. how large the file is,  or if you may have added to it.   The amount of data to
  72. be written will be taken from the BX and CX registers.
  73.  
  74.      If  we want to change one of the registers,  we enter R and  the  register
  75. name.  Let's place 1234 (hexadecimal) in the AX register:
  76.  
  77.      -R AX          R and AX register
  78.      AX 0000        Debug responds with register and contents
  79.      : 1234         : is the prompt for entering new contents.  We respond 1234
  80.      -              Debug is waiting for the next command.
  81.  
  82.      Now if we display the registers, we see the following:
  83.  
  84. AX=1234  BX=0000  CX=0446  DX=0000  SP=FFFE  BP=0000  SI=0000  DI=0000
  85. DS=6897  ES=6897  SS=6897  CS=6897  IP=0100   NV UP DI PL NZ NA PE NC
  86. 6897:0100 E96B01        JMP     026E
  87.  
  88. Note that nothing has changed,  with the exception of the AX register.  The new
  89. value has been placed in it,  as we requested.  One note.  The Register command
  90. can only be used for 16 bit registers (AX,  BX,  etc.).  It cannot change the 8
  91. bit registers (AH,  AL,  BH,  etc.).  To change just AH, for instance, you must
  92. enter the the data in the AX register, with your new AH and the old AL values.
  93.  
  94.  
  95.                                  Dump command
  96.  
  97.      One of the other main features of DEBUG is the ability to display areas of
  98. storage.   Unless you are real good at reading 8088 machine language,  the Dump
  99. command is mostly used to display data (text,  flags,  etc.).  To display code,
  100. the Unassemble command below is a better choice.   If we enter the Dump command
  101. at this time,  DEBUG will default to the start of the program.   It uses the DS
  102. register as it's default,  and,  since this is a .COM file,  begins at DS:0100.
  103. It will by default display 80h (128d) bytes of data, or the length you specify.
  104. The  next  execution of the Dump command will display the following 80h  bytes,
  105. and so on.   For example, the first execution of D will display DS:0100 for 80h
  106. bytes,  the next one DS:0180 for 80h bytes,  etc.   Of course, absolute segment
  107. and  segment register overrides can be used,  but only hex numbers can be  used
  108. for the offset.  That is, D DS:BX is invalid.
  109.  
  110.      With our program loaded, if we enter the Dump command, we will see this:
  111.  
  112. 6897:0100  E9 6B 01 43 4C 4F 43 4B-2E 41 53 4D 43 6F 70 79   ik.CLOCK.ASMCopy
  113. 6897:0110  72 69 67 68 74 20 28 43-29 20 31 39 38 33 4A 65   right (C) 1983Je
  114. 6897:0120  72 72 79 20 44 2E 20 53-74 75 63 6B 6C 65 50 75   rry D. StucklePu
  115. 6897:0130  62 6C 69 63 20 64 6F 6D-61 69 6E 20 73 6F 66 74   blic domain soft
  116. 6897:0140  77 61 72 65 00 00 00 00-00 00 00 00 00 00 00 00   ware............
  117. 6897:0150  00 00 00 00 00 00 00 00-00 24 00 00 00 00 00 00   .........$......
  118. 6897:0160  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00   ................
  119. 6897:0170  00 00 00 00 00 00 00 00-00 00 00 00 44 4F 53 20   ............DOS
  120.  
  121.      Notice that the output from the Dump command is divided into three  parts.
  122. On the left, we have the address of the first byte on the line.  This is in the
  123. format Segment:Offset.
  124.  
  125.      Next  comes the  hex data at that location.   Debug will always start  the
  126. second line at a 16 byte boundary; that is, if you entered D 109, you would get
  127. 7  bytes of information on the first line (109-10F),  and the second line would
  128. start at 110.   The last line of data would have the remaining 9 bytes of data,
  129. so 80h bytes are still displayed.
  130.  
  131.      The third area is the ASCII representation of the data.  Only the standard
  132. ASCII  character set is displayed.   Special characters for the IBMPC  are  not
  133. displayed;  rather periods (.) are shown in their place.   This makes searching
  134. for plain text much easier to do.
  135.  
  136.      Dump can be used to display up to 64K bytes of data, with one restriction:
  137. It cannot cross a segment boundary.   That is,  D 0100 l f000 is valid (display
  138. DS:0100  to  DS:F0FF),  but  D 9000 l 8000 is not (8000h +9000h  =  11000h  and
  139. crosses a segment boundary).
  140.  
  141.      Since  64K  is 10000h and cannot fit into four hex characters,  Dump  uses
  142. 0000 to indicate 64K.  To display a complete segment, enter D 0 l 0.  This will
  143. display the total 64K segment.
  144.  
  145.      If,  at  any time you want to suspend the display  of  data,  Cntl-NumLock
  146. works as usual.   If you want to terminate the display, Cntl-Break will stop it
  147. and return you to the DEBUG prompt.
  148.  
  149.  
  150.  
  151.                                     Search
  152.  
  153.      Search  is  used  to find the occurrence of a specific byte or  series  of
  154. bytes  within a segment.   The address parameters are the same as for the  Dump
  155. command, so we will not duplicate them here.  However, we also need the data to
  156. be searched for.   This data can be entered as either hexadecimal or  character
  157. data.   Hexadecimal  data is entered as bytes,  with a space or a comma as  the
  158. separator.   Character  data is enclosed by single or double quotes.   Hex  and
  159. character data can be mixed in the same request,  i.e. S 0 l 100 12 34 'abc' 56
  160. is  valid,  and requests a search from DS:0000 through DS:00FF for the sequence
  161. of 12h 34h a b c 56h,  in that order.  Upper case characters are different than
  162. lower  case  characters,  and a match will not be found if the  case  does  not
  163. match.   For  instance,  'ABC'  is not the same as 'abc' or 'Abc' or any  other
  164. combination of upper and lower case characters.  However, 'ABC' is identical to
  165. "ABC", since the single and double quotes are separators only.
  166.  
  167.      An example is looking for the string 'Sat'.  Here's what would happen:
  168.  
  169.      -S 0 l 0 'Sat'
  170.      6897:0235
  171.      -
  172. Again,  the  actual segment would be different in your system,  but the  offset
  173. should be the same.   If we then displayed the data,  we would find the  string
  174. 'Saturday'  at this location.   We could also search on 'turda',  or any  other
  175. combination  of characters in the string.   If we wanted to find every place we
  176. did an Int 21h (machine code for Int is CD), we would do the following:
  177.  
  178.      -S 0 l 0 cd 21
  179.      6897:0050
  180.      6897:0274
  181.      6897:027F
  182.      6897:028B
  183.      6897:02AD
  184.      6897:02B4
  185.      6897:0332
  186.      6897:0345
  187.      6897:034C
  188.      6897:043A
  189.      6897:0467
  190.      6897:047A
  191.      6897:0513
  192.      6897:0526
  193.      6897:0537
  194.      6897:0544
  195.      -
  196.  
  197. DEBUG found the hex data CD 21 at the above locations.  This does not mean that
  198. all these addresses are INT 21's, only that that data was there.  It could (and
  199. most likely is) an instruction,  but it could also be an address, the last part
  200. of a JMP instruction,  etc.  You will have to manually inspect the code at that
  201. area to make sure it is an INT 21.   (You don't expect the machine to do every-
  202. thing, do you?).
  203.  
  204.  
  205.                                 Compare command
  206.  
  207.      Along  the  same lines of Dump and Search commands,  we have  the  Compare
  208. command.   Compare  will take two blocks of memory and compare them,  byte  for
  209. byte.  If the two addresses do not contain the same information, both addresses
  210. are displayed,  with their respective data bytes.   As an example, we will com-
  211. pare DS:0100 with DS:0200 for a length of 8.
  212.  
  213.      -d 0100 l 8 0200
  214.      6897:0100  E9  65  6897:0200
  215.      6897:0101  6B  70  6897:0201
  216.      6897:0102  01  74  6897:0202
  217.      6897:0103  43  65  6897:0203
  218.      6897:0104  4C  6D  6897:0204
  219.      6897:0105  4F  62  6897:0205
  220.      6897:0106  43  65  6897:0206
  221.      6897:0107  4B  72  6897:0207
  222.  
  223.      None of the eight bytes compared,  so we got output for each byte.   If we
  224. had gotten a match on any of the bytes, DEBUG would have skipped that byte.  if
  225. all of the locations requested matched,  DEBUG would have simply responded with
  226. another prompt.   No other message is displayed.   This is useful for comparing
  227. two blocks of data from a file,  or a program with the BIOS ROM.   Otherwise, I
  228. have not found a great amount of use for it.
  229.  
  230.  
  231.  
  232.                               Unassemble command
  233.  
  234.      For  debugging,  one of the main commands you will use is  the  Unassemble
  235. command.   This  command will take machine code and convert it to instructions.
  236. Addressing is the same as for previous commands with one exception:   Since  we
  237. are  now  working with code (the previous commands are mainly  for  data),  the
  238. default register is the CS register.  In a .COM program, this makes very little
  239. difference,  unless  you reset the DS register yourself.   However,  in a  .EXE
  240. file,  it  can make a lot of difference,  as the CS and DS registers are set to
  241. different values.
  242.  
  243.      Unassemble data can lead to some interesting results.   For  instance,  in
  244. our example,  CS:IP is set to 6897:0100.   If we look at the program,  we see a
  245. JMP as the first instruction,  followed by data.   If we just enter U,  we will
  246. start at CS:IP (6897:0100) and start unassembling data.   What we will get is a
  247. good instruction, followed by more or less nonsense.  For instance:
  248.  
  249.      -U
  250.      6897:0100 E96B01        JMP     026E
  251.      6897:0103 43            INC     BX
  252.      6897:0104 4C            DEC     SP
  253.      6897:0105 4F            DEC     DI
  254.      6897:0106 43            INC     BX
  255.      6897:0107 4B            DEC     BX
  256.  
  257. And so on,  through 6897:011D.   We know the INC BX, DEC SP, etc. are not valid
  258. instructions,  but  DEBUG doesn't,  so we do have to look at the  code.   After
  259. working with DEBUG a little, you will be able to spot code versus data with the
  260. Unassemble  command.   For now,  suffice to say that the first instruction will
  261. take us to CS:026E and we can start from there.
  262.  
  263.      If we Unassemble CS:026E, we will find something which looks a little more
  264. like what we expect.  We get:
  265.  
  266.      -U 26E
  267.      6897:026E 8D167802      LEA     DX,[0278]
  268.      6897:0272 B409          MOV     AH,09
  269.      6897:0274 CD21          INT     21
  270.      6897:0276 EB05          JMP     027D
  271.      6897:0278 1B5B32        SBB     BX,[BP+DI+32]
  272.      6897:027B 4A            DEC     DX
  273.      6897:027C 24B4          AND     AL,B4
  274.      6897:027E 30CD          XOR     CH,CL
  275.      6897:0280 213C          AND     [SI],DI
  276.      6897:0282 027D0A        ADD     BH,[DI+0A]
  277.      6897:0285 8D167C01      LEA     DX,[017C]
  278.      6897:0289 B409          MOV     AH,09
  279.      6897:028B CD21          INT     21
  280.      6897:028D CD20          INT     20
  281.  
  282.      The first few instructions look fine.   But,  after the JMP  027D,  things
  283. start to look a little funny.  Also, note that there is no instruction starting
  284. at 027D.   We have instructions at 027C and 027E,  but not 027D.  This is again
  285. because DEBUG doesn't know data from instructions.  At 027C, we should (and do)
  286. have the end of our data.   But, this also translates into a valid AND instruc-
  287. tion,  so DEBUG will treat it as such.   If we wanted the actual instruction at
  288. 027D,  we could enter U 027D and get it,  but from here,  we don't know what it
  289. is.   what I'm trying to say is,  DEBUG will do what ever you tell it.   If you
  290. tell it to Unassemble data,  it will do so to the best of its ability.  So, you
  291. have to make sure you have instructions where you think you do.
  292.  
  293.  
  294.                               DATA ENTRY COMMANDS
  295.  
  296.                                      Enter
  297.  
  298.      The  Enter command is used to place bytes of data in memory.   It has  two
  299. modes:  Display/Modify  and  Replace.   The difference is in where the data  is
  300. specified - in the Enter command itself, or after the prompt.
  301.  
  302.      If you enter E address alone,  you are in display/modify mode.  DEBUG will
  303. prompt  you  one  byte at a time,  displaying the current byte  followed  by  a
  304. period.   At this time,  you have the option of entering one or two hexadecimal
  305. characters.   If you hit the space bar, DEBUG will not modify the current byte,
  306. but  go on to the next byte of data.   If you go too far,  the hyphen (-)  will
  307. back up one byte each time it is pressed.
  308.  
  309.      E 103
  310.      6897:0103  43.41   4C.42   4F.43   43.     4B.45
  311.      6897:0108  2E.46   41.40   53.-
  312.      6897:0109  40.47   53.
  313.  
  314. In this example,  we entered E 103.   DEBUG responded with the address and  the
  315. information  at  that  byte (43).   We entered the 41 and  DEBUG  automatically
  316. showed the next byte of data (4C).  Again, we entered 42, debug came back.  The
  317. next byte was 4F, we changed it to 43.  At 106, 43 was fine with us, so we just
  318. hit the space bar.  DEBUG did not change the data, and went on to the following
  319. bytes.  After entering 40 at location 109, we found we had entered a bad value.
  320. The  hyphen  key was pressed,  and DEBUG backed up  one  byte,  displaying  the
  321. address and current contents.  Note that it has changed from the original value
  322. (41)  to  the value we typed in (40).   We then type in the correct  value  and
  323. terminate by pressing the ENTER key.
  324.  
  325.      As you can see,  this can be very awkward,  especially where large amounts
  326. of data are concerned.   Also, if you need ASCII data, you have to look up each
  327. character  and enter its hex value.   Not easy,  to be sure.   That's where the
  328. Replace  mod  of operation comes in handy.   Where the Display/Modify  mode  is
  329. handy  for  changing a few bytes at various offsets,  the Replace mode  is  for
  330. changing  several  bytes of information at one time.   Data can be  entered  in
  331. hexadecimal or character format,  and multiple bytes can be entered at one time
  332. without  waiting  for the prompt.   If you wanted to store the  characters  'My
  333. name' followed by a hexadecimal 00 starting at location 103, you would enter:
  334.  
  335.      E 103 'My name' 0
  336.  
  337. As  in  the Search command,  data can be entered in character  (in  quotes)  or
  338. hexadecimal  forms  and  can be mixed in the same command.   This is  the  most
  339. useful way of entering large amounts of data into memory.
  340.  
  341.  
  342.                                      Fill
  343.  
  344.      The Fill command is useful for storing a lot of data of the same data.  It
  345. differs  from  the Enter command in that the list will be  repeated  until  the
  346. requested amount of memory is filled.  If the list is longer than the amount of
  347. memory to be filled,  the extra items are ignored.   Like the Enter command, it
  348. will  take hexadecimal or character data.   Unlike the Enter  command,  though,
  349. large amounts of data can be stored without specifying every character.   As an
  350. example, to clear 32K (8000h) of memory to 00h, you only need to enter:
  351.  
  352.      F 0 L 8000 0
  353.  
  354. Which translates into Fill, starting at DS:0000 for a Length of 32K (8000) with
  355. 00h.   If the data were entered as '1234',  the memory would be filled with the
  356. repeating string '123412341234',  etc.   Usually,  it is better to enter  small
  357. amounts of data with the Enter command,  because an error in the length parame-
  358. ter of the Fill command can destroy a lot of work.  The Enter command, however,
  359. will  only change the number of bytes actually entered,  minimizing the effects
  360. of a parameter error.
  361.  
  362.  
  363.                                      Move
  364.  
  365.      The Move command does just what it says - it moves data around inside  the
  366. machine.   It  takes  bytes from with the starting address and moves it to  the
  367. ending address.   If you need to add an instruction into a program,  it can  be
  368. used  to make room for the instruction.   Beware,  though.   Any data or labels
  369. referenced after the move will not be in the same place.   Move can be used  to
  370. save a part of the program in free memory while you play with the program,  and
  371. restore  it  at any time.   It can also be used to copy ROM BIOS  into  memory,
  372. where it can be written to a file or played with to your heart's content.   You
  373. can then change things around in BIOS without having to worry about programming
  374. a ROM.
  375.  
  376.      M 100 L 200 ES:100
  377.  
  378. This  will  move the data from DS:0100 to DS:02FF (Length 200) to  the  address
  379. pointed to by ES:0100.  Later, if we want to restore the data, we can say:
  380.  
  381.      M ES:100 L 200 100
  382.  
  383. which will move the data back to its starting point.   Unless the data has been
  384. changed while at the temporary location (ES:0100),  we will restore the data to
  385. its original state.
  386.  
  387.                                    Assemble
  388.  
  389. I purposely left the Assemble command to the end,  as it is the most complex of
  390. the  data entry commands.   It will take the instructions in the assembler lan-
  391. guage and convert them to machine code directly.   Some of the things it  can't
  392. do,  however,  are: reference labels, set equates, use macros, or anything else
  393. which cannot be translated to a value.  Data locations have to be referenced by
  394. the physical memory address, segment registers, if different from the defaults,
  395. must be specified,  and RET instructions must specify the type (NEAR or FAR) of
  396. return to be used.   Also,  if an instruction references data but not registers
  397. (i.e.  Mov [278],5), the Byte ptr or Word ptr overrides must be specified.  One
  398. other  restriction:   To tell DEBUG the difference between moving 1234h into AX
  399. and  moving  the data from location 1234 into AX,  the latter is coded  as  Mov
  400. AX,[1234],  where the brackets indicate the reference is an addressed location.
  401. The differences between MASM and DEBUG are as follows:
  402.  
  403.      MASM                DEBUG                    Comments
  404.  
  405.      Mov  AX,1234        Mov  AX,1234             Place 1234 into AX
  406.      Mov  AX,L1234       Mov  AX,[1234]           Contents of add. 1234 to AX
  407.      Mov  AX,CS:1234     CS:Mov AX,[1234]         Move from offset of CS.
  408.      Movs Byte ptr ...   Movesb                   Move byte string
  409.      Movs Word ptr ...   Movsw                    Move word string
  410.      Ret                 Ret                      Near return
  411.      Ret                 Retf                     Far return
  412.  
  413. Also,  Jmp instructions will be assembled automatically to Short,  Near, or Far
  414. Jmps.  However, the Near and Far operands can be used to override the displace-
  415. ment if you do need them.  Let's try a very simple routine to clear the screen.
  416.  
  417.      -A 100
  418.      6897:0100 mov ax,600
  419.      6897:0103 mov cx,0
  420.      6897:0106 mov dx,184f
  421.      6897:0109 mov bh,07
  422.      6897:010B int 10
  423.      6897:010D int 20
  424.      6897:010F
  425.      -
  426.  
  427.      We  are using BIOS interrupt 10h,  which is the video interrupt.  (If  you
  428. would like more information on the interrupt,  there is a very good description
  429. in  the Technical Reference Manual.)  We need to call BIOS with  AX=600,  BH=7,
  430. CX=0, and DX=184Fh.  First we had to load the registers, which we did at in the
  431. first  four instructions.   The statement at offset 6897:010B  actually  called
  432. BIOS.   The INT 20 at offset 010D is for safety only.  We really don't need it,
  433. but with it in,  the program will stop automatically.   Without the INT 20, and
  434. if  we did not stop,  DEBUG would try and execute whatever occurs at 010F.   If
  435. this  happens  to  be a valid program (unlikely),  we would  just  execute  the
  436. program.   Usually,  though,  we will find it to be invalid,  and will probably
  437. hang the system,  requiring a cntl-alt-del (maybe) or a power-off and on  again
  438. (usually).  So, be careful and double check your work!
  439.  
  440.      Now, we need to execute the program.  To do this, enter the G command, a G
  441. followed  by  the enter key.   If you have entered the program  correctly,  the
  442. screen  will  clear and you will get a message "Program  terminated  normally".
  443. (More on the Go command later).
  444.  
  445.      Again, I cannot stress the importance of checking your work when using the
  446. Assemble  command.   The commands may assemble correctly,  but cause a  lot  of
  447. problems.   This  is especially important for the Jmp and Call commands;  since
  448. they  cause  an interruption in the flow of the program,  they  can  cause  the
  449. program  to jump into the middle of an instruction,  causing VERY unpredictable
  450. results.
  451.  
  452.  
  453.                                  I/O commands
  454.  
  455.                                      Name
  456.  
  457. The  Name  command has just one purpose - specifying the name of a  file  which
  458. DEBUG is going to Load or Write.  It does nothing to change memory or execute a
  459. program,  but does prepare a file control block for DEBUG to work with.  If you
  460. are going to load a program,  you can specify any parameters on the same  line,
  461. just  like in DOS.   One difference is,  the extension MUST be specified.   The
  462. default is no extension.   DEBUG will load or write any file, but the full file
  463. name must be entered.
  464.  
  465.      -n chkdsk.com /f
  466.  
  467. This statement prepares DEBUG for loading the program CHKDSK.COM passing the /f
  468. switch to the program.   When the Load (see below) command is  executed,  DEBUG
  469. will  load CHKDSK.COM and set up the parameter list (/f) in the program's input
  470. area.
  471.  
  472.  
  473.                                      Load
  474.  
  475.      The Load command has two formats.  The first one will load a program which
  476. has been specified by the Name command into storage, set the various registers,
  477. and prepare for execution.   Any program parameters in the Name command will be
  478. set into the Program Segment Prefix,  and the program will be ready to run.  If
  479. the  file is a .HEX file,  it is assumed to have valid  hexadecimal  characters
  480. representing  memory  values,  two hexadecimal characters per byte.  Files  are
  481. loaded  starting at CS:0100 or at the address specified in the  command.    For
  482. .COM.  .HEX and .EXE files,  the program will be loaded, the registers set, and
  483. CS:IP  set  to the first instruction in the  program.   For  other  files,  the
  484. registers are undetermined, but basically, the segment registers are set to the
  485. segment of the PSP (100h bytes before the code is actually loaded),  and BX and
  486. CX are set to the file length.  Other registers are undetermined
  487.  
  488.      -n clock.com
  489.      -l
  490.  
  491. This  sequence  will load clock.com into memory,  set IP to the entry point  of
  492. 0100,  and CX will contain 0446, the hexadecimal size of the file.  The program
  493. is now ready to run.
  494.  
  495.      The second form of the Load command does not use the Name command.   It is
  496. used  to load absolute sectors from the disk (hard or soft) into  memory.   The
  497. sector count starts with the first sector of track 0 and continuing to the  end
  498. of the track.   The next sector is track 0,  second side (if double sided), and
  499. continues to the end of that sector.   Then,  back to the first side,  track 1,
  500. and so on,  until the end of the disk.   Up to 80h (128d) sectors can be loaded
  501. at  one time.   To use,  you must specify starting address,  drive  (0=A,  1=B,
  502. etc.), starting sector, and number of sectors to load.
  503.  
  504.      -l 100 0 10 20
  505.  
  506. This instruction tells DEBUG to load, starting at DS:0100, from drive A, sector
  507. 10h  for 20h sectors.   DEBUG can sometimes be used this way to recover part of
  508. the  information on a damaged sector.   If you get an error,  check the  memory
  509. location  for that data.   Often times,  part of the data has been  transferred
  510. before  the error occurs and the remainder (especially for text files)  can  be
  511. manually entered.   Also,  repetitive retrys will sometimes get the information
  512. into  memory.   This can then be rewritten on the same diskette (see the  Write
  513. command below), or copied to the same sector on another diskette.  In this way,
  514. the data on a damaged disk can sometimes be recovered.
  515.  
  516.  
  517.                                      Write
  518.  
  519.      The  write  command is very similar to the Load command.   Both  have  two
  520. modes of operation, and both will operate on files or absolute sectors.  As you
  521. have  probably guessed,  the Write command is the opposite of the Load command.
  522. Since all the parameters are the same,  we will not cover the syntax in detail.
  523. However,  one  thing worth mentioning:  When using the file mode of  the  Write
  524. command,  the  amount of data to be written is specified in BX and CX,  with BX
  525. containing the high-order file size.   The start address can be specified or is
  526. defaulted to CS:0100.   Also, files with an extension of .EXE or .HEX cannot be
  527. written  out,  and error message to that effect will be displayed.   If you  do
  528. need  to  change a .EXE or .HEX file,  simply rename and  load  it,  make  your
  529. changes, save it and name it back to its original filename.
  530.  
  531.  
  532.                                    Input
  533.  
  534.      The  Input command can be used to read a byte of data from any of the  I/O
  535. ports  in  the PC.   The port address can be either a one or two byte  address.
  536. DEBUG will read the port, and display the contents.
  537.  
  538.      -i 3fd
  539.      7D
  540.      -
  541.  
  542. This is the Line input port for the first Asynchronous adapter.   Your data may
  543. be different,  as it depends on the current status of the port.   It  indicates
  544. the  data  in the register at the time it was read was 7Dh.   Depending on  the
  545. port, this data may change, as the ports are not controlled by the PC.
  546.  
  547.  
  548.                                     Output
  549.  
  550.      As you can probably guess,  the Output command is the reverse of the Input
  551. command.   You  can use the Output command to send a single byte of data  to  a
  552. port.   Note  that certain ports can cause the system to hang (especially those
  553. dealing with system interrupts and the keyboard),  so be careful with what  you
  554. send where!
  555.  
  556.      -o 3fc 1
  557.      -
  558.  
  559. Port 3FCh is the modem control register for the first asynchronous port.  Send-
  560. ing a 01h to this port turns on the DTR (Data Terminal Ready) bit.   A 00h will
  561. turn all the bits off.   If you have a modem which indicates this bit,  you can
  562. watch the light flash as you turn the bit on and off.
  563.  
  564.  
  565.                               EXECUTION COMMANDS
  566.  
  567.                                       Go
  568.  
  569.      The  Go  command  is used to start program execution.   A  very  versatile
  570. command, it can be used to start the execution at any point in the program, and
  571. optionally  stop  at any of ten points (breakpoints) in  the  program.   If  no
  572. breakpoints  are set (or the breakpoints are not executed),  program  execution
  573. continues  until  termination,  in which case the message  "Program  terminated
  574. normally" is sent.   If a breakpoint is executed,  program execution stops, the
  575. current registers are displayed, and the DEBUG prompt is displayed.  Any of the
  576. DEBUG commands can be executed, including the Go command to continue execution.
  577. Note  that the Go command CANNOT be terminated by Cntl-break.   This is one  of
  578. the few commands which cannot be interrupted while executing.
  579.  
  580.      -g =100
  581.  
  582. The  Go command without breakpoints starts program execution at the address (in
  583. this  case  CS:0100)  in the command.   The equal sign before  the  address  is
  584. required.   (Without the equal sign, the address is taken as a breakpoint.)  If
  585. no starting address is specified,  program execution starts at CS:IP.   In this
  586. case,  since  no breakpoints are specified,  CLOCK.COM will continue  execution
  587. until the cntl-break key is pressed and the program terminates.   At this time,
  588. you will get the message "Program terminated normally".   Note that,  after the
  589. termination  message,  the  program should be reloaded before  being  executed.
  590. Also,  any memory alterations (storing data,  etc.) will not be restored unless
  591. the program is reloaded.
  592.  
  593.      -g 276 47c 528 347
  594.  
  595. This version of the control command will start the program and set  breakpoints
  596. at  CS:276,  CS:47C,  CS:528  and  CS:347.   These correspond to  locations  in
  597. CLOCK.COM  after the screen is cleared,  and the day,  date and time  are  dis-
  598. played,  respectively.   The  program will stop at whichever breakpoint it hits
  599. first.   Note  that the second and third breakpoints will only be displayed  at
  600. two times - when the program is started and at midnight.   If you care to  stay
  601. up (or just change the time in the computer),  and set a breakpoint at 47C,  t
  602. will stop when the program is started, and again at midnight.
  603.  
  604.      Some notes about breakpoints. The execution stops just before the instruc-
  605. tion is executed.  Setting a breakpoint at the current instruction address will
  606. not execute any instructions.  DEBUG will set the breakpoint first, then try to
  607. execute the instruction, causing another breakpoint.  Also, the breakpoints use
  608. Interrupt  3 to stop execution.   DEBUG intercepts interrupt 3 to stop the pro-
  609. gram execution and display the registers.   Finally,  breakpoints are not saved
  610. between Go commands.  Any breakpoints you want will be have to be set with each
  611. Go command.
  612.  
  613.  
  614.                                      Trace
  615.  
  616.      Along the same lines as Go is the Trace command.   The difference is that,
  617. while Go executes a whole block of code at one time, the Trace command executes
  618. instructions  one at a time,  displaying the registers after each  instruction.
  619. Like the Go instruction,  execution can be started at any address.   The  start
  620. address again must be preceeded by an equal sign.   However,  the Trace command
  621. also has a parameter to indicate how many instructions are to be executed.
  622.  
  623.      -t =100 5
  624.  
  625. This Trace command will start at CS:100 and execute five instructions.  Without
  626. the  address,  execution will start at the current CS:IP value and continue for
  627. five instructions.  T alone will execute one instruction.
  628.  
  629.      When using Trace to follow a program, it is best to go around calls to DOS
  630. and interrupts,  as some of the routines involved can be  lengthy.   Also,  DOS
  631. cannot be Traced,  and doing so has a tendency to hang the system.   Therefore,
  632. Trace  to  the call or interrupt and Go to the next address after the  call  or
  633. interrupt.
  634.  
  635.  
  636.                               ARITHMETIC COMMANDS
  637.  
  638.                                  Hexarithmetic
  639.  
  640.      The  Hexarithmetic command is handy for adding and subtracting hexadecimal
  641. numbers.  It has just two parameters - the two numbers to be added and subtrac-
  642. ted.   DEBUG's response is the sum and difference of the numbers.   The numbers
  643. can be one to four hexadecimal digits long.   The addition and subtraction  are
  644. unsigned, and no carry or borrow is shown beyond the fourth (high order) digit.
  645.  
  646.      -h 5 6
  647.      000B FFFF
  648.      -h 5678 1234
  649.      68AC 4444
  650.      -
  651.  
  652. In  the  first example,  we are adding 0005 and 0006.   The sum  is  000B,  the
  653. difference  is -1.   However,  since there is no carry,  we get FFFF.   In  the
  654. second example, the sum of 5678 and 1234 is 68AC, and the difference is 4444.
  655.  
  656.  
  657.                                     WRAPUP
  658.  
  659.      If  you give it a chance,  DEBUG can be a very useful tool for the  IBMPC.
  660. It  is  almost a requirement for debugging assembler language programs,  as  no
  661. nice  error  messages are produced at run time.   DEBUG does work at  the  base
  662. machine  level,  so you need some experience to use it  effectively,  but  with
  663. practice, it will be your most useful assembler language debugging tool.
  664.  
  665.